package com.inter.law.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;



import com.inter.law.entity.*;
import com.inter.law.dao.IlCaseDAO;
import com.inter.law.dao.IlCaseToClientDAO;
import com.inter.law.dao.IlClientDAO;
import com.inter.law.dao.IlLawerDAO;
import com.inter.law.dao.IlLawerToClientDAO;
import com.inter.law.dao.IlLawerToContrayClientDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 申请立案时冲突检测
 * @author Administrator
 *
 */
public class CollisionDetection extends ActionSupport{
	private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	private String json;
	private JSONObject jsonObject=new JSONObject();
	private List<String> client;
	private List<String> contrayClient;
	private List<Integer> clientType;
	private List<Integer> contrayClientType;
	 private List<List<String>> content_collision= new ArrayList<List<String>>();
	 Map<String, Object> session = ActionContext.getContext().getSession();
	 private String caseName;
	@Override
	public String execute() throws Exception {
		IlClientDAO dao=IlClientDAO.getFromApplicationContext(ctx);
		IlCaseDAO casedao=IlCaseDAO.getFromApplicationContext(ctx);
		IlCaseToClientDAO casetoclientdao=IlCaseToClientDAO.getFromApplicationContext(ctx);
		IlLawerDAO lawerdao=IlLawerDAO.getFromApplicationContext(ctx);
		Integer num=1;
		int listsLength=0;
		for(int i=0;i<contrayClient.size();i++){
			List list=dao.findByNameAndType(contrayClient.get(i), contrayClientType.get(i)); //找到当事人列表，其实只会找到一个，没必要用列表
			listsLength+=list.size();
			//for(int j = 0;i<list.size();j++){
			if(list.size()>0){
				int client_id=((IlClient)list.get(0)).getId();								//获得当事人id
				List list_case_id = casetoclientdao.findByClientId(client_id);			//根据当事人id找到案件id
				for(int j = 0;j<list_case_id.size();j++){					//根据caseid在案件中查找案件
					int case_id=((IlCaseToClient)list_case_id.get(j)).getCaseId();
					List<String> list_collison = new ArrayList<String>();
					list_collison.add(num.toString());			//添加序号
					num++;
					list_collison.add(contrayClient.get(i));				//添加对方当事人姓名
					System.out.print("case_id: "+case_id+"\n");
					list_collison.add(casedao.findById(case_id).getCasePreType());//添加对方当事人(作为我方委托人时)的案件类别
					IlLawer lawer = lawerdao.findById(casedao.findById(case_id).getCaseLawerId());   //找到律师
						list_collison.add(lawer.getLawerName());					//添加律师姓名
					int status = casedao.findById(case_id).getCaseStatus();
					if(status==0)											//0--案件办理中
						list_collison.add("案件办理中");
					else if(status==1)
						list_collison.add("已结案");							//1--已结案
					content_collision.add(list_collison);					//一条记录，插入到二维list中*/
				}
			}
			//System.out.println(contrayClientType.get(i));
		}
		if(listsLength==0){
			System.out.println("没有冲突");
			jsonObject.put("message", 0);
			json=jsonObject.toString();
			return SUCCESS;  
		}else{
			System.out.println("有冲突");
			jsonObject.put("message", 1);
			json=jsonObject.toString();
			return ERROR;  
		}
        
	}
	
	public String executeSuccess() throws Exception {
		String username=(String) session.get("username");
		IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ctx);
		ArrayList list=(ArrayList) ilLawerDAO.findByLawerUsername(username);
		Integer id=0;
		if(list!=null&&list.size()>0){
			id=((IlLawer)(list.get(0))).getLawerId();
			System.out.println(id+"AAAAAAAAAA");
		}
		if(id!=0){
			IlLawerToClientDAO ilLawerToClientDAO=IlLawerToClientDAO.getFromApplicationContext(ctx);
			IlLawerToContrayClientDAO ilLawerToContrayClientDAO=IlLawerToContrayClientDAO.getFromApplicationContext(ctx);
			for(int i=0;i<client.size();i++){
				List<IlLawerToClient> ilLawerToClients=ilLawerToClientDAO.findByName(client.get(i));
				if(ilLawerToClients!=null&&ilLawerToClients.size()>0){
					
				}else{
					IlLawerToClient ilLawerToClient=new IlLawerToClient();
					ilLawerToClient.setLawerId(id);
					System.out.println(id+"AAASSSSSSSSSA");
					ilLawerToClient.setName(client.get(i));
					ilLawerToClientDAO.save(ilLawerToClient);
				}
			}
            for(int i=0;i<contrayClient.size();i++){
            	List<IlLawerToContrayClient> ilLawerToContrayClients=ilLawerToContrayClientDAO.findByName(contrayClient.get(i));
				if(ilLawerToContrayClients!=null&&ilLawerToContrayClients.size()>0){
					
				}else{
					IlLawerToContrayClient ilLawerToContrayClient=new IlLawerToContrayClient();
					ilLawerToContrayClient.setLawerId(id);
					ilLawerToContrayClient.setName(client.get(i));
					ilLawerToContrayClientDAO.save(ilLawerToContrayClient);
				}
			}
		}
		caseName="";
		for(int i=0;i<client.size();i++){
			if(i!=client.size()-1){
				caseName+=client.get(i)+",";
			}else{
				caseName+=client.get(i);
			}
			
		}
		caseName+="与";
        for(int i=0;i<contrayClient.size();i++){
        	if(i!=contrayClient.size()-1){
				caseName+=contrayClient.get(i)+",";
			}else{
				caseName+=contrayClient.get(i);
			}
		}
        caseName+="的XX案件";
		return SUCCESS;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}

	public List<String> getClient() {
		return client;
	}

	public void setClient(List<String> client) {
		this.client = client;
	}

	public List<String> getContrayClient() {
		return contrayClient;
	}

	public void setContrayClient(List<String> contrayClient) {
		this.contrayClient = contrayClient;
	}

	public List<Integer> getClientType() {
		return clientType;
	}

	public void setClientType(List<Integer> clientType) {
		this.clientType = clientType;
	}

	public List<Integer> getContrayClientType() {
		return contrayClientType;
	}

	public void setContrayClientType(List<Integer> contrayClientType) {
		this.contrayClientType = contrayClientType;
	}

	public List<List<String>> getContent_collision() {
		return content_collision;
	}

	public void setContent_collision(List<List<String>> content_collision) {
		this.content_collision = content_collision;
	}

	public String getCaseName() {
		return caseName;
	}

	public void setCaseName(String caseName) {
		this.caseName = caseName;
	}

	
	
	
}
